package ch02;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.servlet.annotation.WebServlet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Jane
 * @date 2024-03-18 15:11
 */

public class TestMybatisConfig {
    String url = "src/main/resources/UserMapper.xml";
    Document doc = null;

    @Before  //在...之前
    public void init() throws ParserConfigurationException, IOException, SAXException {
        //加载xml文件
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = fac.newDocumentBuilder();
        doc = builder.parse(url);
        System.out.println("文档加载成功");
    }

    @Test
    public void xmlParse() throws ParserConfigurationException, IOException, SAXException {
        //文件路径
        //从工厂里面拿到一个可以用来解析xml的对象
        //加载文件
        //读取节点(指定节点读取,读取所有节点信息)

        //   String src = "src/main/resources/UserMapper.xml";

        //加载xml文件
  /*      DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = fac.newDocumentBuilder();
        Document doc = db.parse(url);
*/
        //获取所有select的节点信息
        NodeList nodeList = doc.getElementsByTagName("select");

        System.out.println(nodeList.getLength()); //节点数量
        //解析节点内容
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i); //获取单个节点
            Element e =(Element)item;//强转成元素
            NamedNodeMap nodeMap = e.getAttributes(); //获取元素的所有的属性 的键值对
            for (int j = 0; j < nodeMap.getLength(); j++) {
                Node childNode = nodeMap.item(j);//子项
                System.out.println(childNode.getNodeName()+":\t"+childNode.getNodeValue());
            }
            String content = item.getTextContent(); //节点文本内容
            System.out.printf("文本内容:%s\n",content);
            System.out.println("------------------------------------");
        }


/*
        String arrtValue = e.getAttribute("namespace");
        System.out.println(arrtValue);

        String cache = e.getAttribute("cache");
        System.out.println(cache);*/

    }


    @Test
    public void addNode(){
        System.out.println("添加节点信息");
        Element select_count =doc.createElement("select");
        select_count.setAttribute("id","getCount");
        select_count.setTextContent("select count(id) from t_user");

        Node mapper = doc.getElementsByTagName("mapper").item(0);
        Element e = (Element)mapper;
        e.appendChild(select_count);

        //保存
        //调用后置方法保存
        System.out.println("节点添加成功");
    }

    @After
    public void destory() throws TransformerException, FileNotFoundException {
        //System.out.println("释放资源");
        TransformerFactory fac =TransformerFactory.newInstance();
        Transformer transformer = fac.newTransformer();
        DOMSource source= new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"utf-8");
        StreamResult res = new StreamResult(new FileOutputStream(url));
        //把dom树转换为xml
        transformer.transform(source,res);
    }
}
